#include "SimulatedMoveAction.hpp"
#include <cassert>

SimulatedMoveAction::SimulatedMoveAction(Chess::Color color, const Position& pos_orig, const Position& pos_dest)
	: MoveAction(color, pos_orig, pos_dest), 
		SimulatedAction()
{}


SimulatedMoveAction::SimulatedMoveAction(const SimulatedMoveAction& smove)
	: MoveAction(smove)
{}

SimulatedMoveAction::~SimulatedMoveAction()
{}


void	SimulatedMoveAction::reset(const Position& pos_orig, const Position& pos_dest)
{
	pos1 = pos_orig;
	pos2 = pos_dest;
	piece_captured = 0;
}

void	SimulatedMoveAction::undo()
{
	try
	{		
		Cell*			src = 0;
		Cell*			dst = 0;
		Piece*			piece = 0;
		
		
		src = Chess::getInstance()->getChessboard()->getCell(pos2);
		dst = Chess::getInstance()->getChessboard()->getCell(pos1);
		
		assert ((piece = src->getPiece()) != 0);
				
		piece->setCell(dst);
		dst->setPiece(piece);
		src->setPiece(0);
		
		if (piece_captured != 0)
		{
			src->setPiece(piece_captured);
			piece_captured->setCell(src);
		}
		
		piece->setNbMovements(piece->getNbMovements() - 1);
	}
	catch(Exception* e)
	{	throw e;	}
}

